package cn.wlinker.video.monitor.utils;

import cn.hutool.core.date.DateTime;

import java.util.HashMap;
import java.util.Map;

/**
 * 流地址缓存
 *
 * @author gxsjx
 * @version 1.0
 * @date 2025/5/3
 * Copyright © wlinker.cn
 */
public class StreamUrlCacheUtils {

    /**
     * 缓存流地址
     *
     * @param streamId
     * @param streamUrl
     */
    private static final Map<String, String> STREAM_URL_CACHE = new HashMap<>();

    /**
     * 创建流id
     *
     * @param host
     * @param channelNo
     * @param startTime
     * @param endTime
     * @return
     */
    private static String createStreamId(String host, Integer channelNo, DateTime startTime, DateTime endTime) {
        return SsrcGenerator.generateHexSsrc(host, channelNo, startTime, endTime);
    }

    /**
     * 缓存流地址
     *
     * @param host
     * @param channelNo
     * @param url
     */
    public static String putAndReturnStreamId(String host, Integer channelNo, String url) {
        return putAndReturnStreamId(host, channelNo, null, null, url);
    }

    /**
     * 缓存流地址
     *
     * @param host
     * @param channelNo
     * @param startTime
     * @param endTime
     * @param url
     */
    public static String putAndReturnStreamId(String host, Integer channelNo, DateTime startTime, DateTime endTime, String url) {
        String streamId = StreamUrlCacheUtils.createStreamId(host, channelNo, startTime, endTime);
        put(streamId, url);
        return streamId;
    }

    /**
     * 设置url缓存
     *
     * @param streamId
     * @param streamUrl
     */
    public static void put(String streamId, String streamUrl) {
        STREAM_URL_CACHE.put(streamId, streamUrl);
    }

    /**
     * 获取url缓存
     *
     * @param streamId
     * @return
     */
    public static String get(String streamId) {
        return STREAM_URL_CACHE.get(streamId);
    }

}
